/*
*** (C) 2008 UCMai.com ***

Subj: 会员管理
Auth: zhouyongtai
Date: 2012/1/6
*/

var userWins = new Ext.WindowGroup();
var membercore = function(){

	var addresscore = function(){
		this.gridHeader = [
			{header: '真实姓名', dataIndex: 'realname'},
			{header: '省份',dataIndex: 'firstregion'},
			{header: '市', dataIndex: 'secondregion'},
			{header: '区/县', dataIndex: 'thirdregion'},
			{header: '详细地址', dataIndex: 'detail'},
			{header: '邮编', dataIndex: 'zipcode'},
			{header: '电话', dataIndex: 'tel'},
			{header: '默认', dataIndex: 'isdefault', renderer: function(value){
				return c.YorN[value];
			}}
		];
		this.gridStore = [{name:"id"},{name: 'realname'},{name: 'firstregion'},{name:'secondregion'},{name:'thirdregion'},{name:'detail'},{name:'zipcode'},{name:'tel'},{name:'isdefault'}];

		addresscore.superclass.constructor.call(this, {
			region: 'center',
			url: _C_Address_X,
			checkbox:false,
			keyField:'id',
			layout: 'fit',
			viewConfig: {forceFit:true}
		});
	};
	Ext.extend(addresscore,Ext.ux.grid.GridPanel);
	//Grid列头
	this.gridHeader = [
		{header:'邮箱地址',dataIndex:'Email',width:200},
		{header:'手机号码',dataIndex:'Mobile'},
		{header:'用户昵称',dataIndex:'UserName'},
		{header:'用户类型',dataIndex:'MemberType'},
		{header:'等级',dataIndex:'Rank'},
		{header: '注册日期', dataIndex: 'RegsterTime'},
		{header: '最后一次登录', dataIndex: 'LastLogin'},
		{header:'最后登录IP', dataIndex: 'LastIp'},
		{header: '登录次数', dataIndex: 'LoginTimes'},
		{header:'是否在线', dataIndex: 'IsOnline'},
		{header:'状态',dataIndex:'LockStatus',renderer:function(value){
			return '<b><font color="'+ c.aLock[value][1] +'">'+ c.aLock[value][0] +'</font></b>';
		}}
	];
	
	//Grid绑定的字段
	this.gridStore = [
		{name:'MemberID'},
		{name:'Email'},
		{name:'Mobile'},
		{name:'UserName'},
		{name:'MemberType'},
		{name:'Rank'},
		{name:'RegsterTime'},
		{name:'LastLogin'},
		{name:'LastIp'},
		{name:'LoginTimes'},
		{name: 'IsOnline'},
		{name:'OptionSet'},
		{name:'Status'},
		{name:'LockStatus'}
	];

	//查看项
	this.viewItems = [{
			columnWidth: .22,
			layout:'form',
			height: 150,
			items:[{
				html: '<img src="http://list.image.baidu.com/t/image_category/galleryimg/womenstar/us/Leona_Lewis.jpg" height="148"/>'
			}]
		
		},{
			columnWidth:.39,
			layout:'form',
			items:[
				{
					xtype:'textfield',
					fieldLabel: '邮箱地址',
					name: 'Email',
					anchor:'90%',
					readOnly:true
				}
			]
		},{
			columnWidth:.39,
			layout:'form',
			items:[
				{
					xtype:'numberfield',
					fieldLabel: '手机号码',
					name: 'Mobile',
					anchor:'90%'
				}
			]
		},{
			columnWidth: .39,
			layout:'form',
			items:[{
				xtype: "textfield",
				fieldLabel: "会员昵称",
				name: 'UserName',
				anchor: '90%',
			}]
		},{
			columnWidth: .39,
			layout: 'form',
			items: [{
				xtype: 'combo',
				store: (function (){var membertype =  new Ext.data.SimpleStore({
						fields:[{name:'MemberType',type:'int'},{name:'MemberTypeText'}]
					});
					membertype.loadData([[0,'个人注册'],[1,'企业注册']]);
					return membertype;
				})(), 
				displayField:'MemberTypeText',
				valueField:'MemberType',
				fieldLabel: '会员类别',
				name: 'MemberType',
				hiddenName: 'MemberType',
				id: 'se_o_membertype',
				mode: 'local',
				triggerAction: 'all',
				blankText:'请选择类别',
				emptyText:'选择类别',
				allowBlank:false,
				anchor:'85%',
			}]
		},{
			columnWidth: .39,
			layout: 'form',
			items: [{
				xtype: 'combo',
				store: (function (){var rank =  new Ext.data.SimpleStore({
						fields:[{name:'RankId',type:'int'},{name:'RankText'}]
					});
					rank.loadData([[0,'普通会员'],[1,'高级会员']]);
					return rank;
				})(), 
				displayField:'RankText',
				valueField:'RankId',
				fieldLabel: '会员等级',
				name: 'Rank',
				hiddenName: 'Rank',
				id: 'se_o_rank',
				mode: 'local',
				triggerAction: 'all',
				blankText:'请选择等级',
				emptyText:'选择等级',
				allowBlank:false,
				anchor:'85%',
			}]
		},{
			columnWidth: .39,
			layout: 'form',
			items: [{
				xtype: 'textfield',
				fieldLabel: '注册日期',
				name: 'RegsterTime',
				anchor: '90%',
				readOnly: true
			}]
		},{
			columnWidth: .39,
			layout: 'form',
			items: [{
				xtype: 'textfield',
				fieldLabel: '最后登录',
				name: 'LastLogin',
				anchor: '90%',
				readOnly: true
			}]
		},{
			columnWidth: .39,
			layout: 'form',
			items: [{
				xtype:'textfield',
				fieldLabel: '登录IP',
				name: 'LastIp',
				anchor: '90%',
				readOnly: true
			}]
		},{
			columnWidth: .39,
			layout: 'form',
			items: [{
				xtype: 'textfield',
				fieldLabel: '登录次数',
				name: 'LoginTimes',
				anchor: '90%',
				readOnly: true
			}]
		},{
			columnWidth: .39,
			layout: 'form',
			items:[{
				xtype: 'textfield',
				fieldLabel: '是否在线',
				name: 'IsOnline',
				anchor: '90%',
				readOnly: true
			}]
		},{
			columnWidth: .39,
			layout: 'form',
			items: [{
				xtype: 'field',
				fieldLabel: '状态',
				name: 'LockStatus',
				anchor: '90%',
				readOnly: true
			}]
		},{
			columnWidth:1,
			layout:'form',
			items:[
				{
					xtype:'tabpanel',
					deferredRender: true,
					activeTab: 0,
					items: [{
						title: '个人信息',
						layout: 'column',
						border:false,
						labelSeparator:'：',
						defaults: {style:'padding: 3px 5px 0px 5px'},
						items: [{
							columnWidth: .5,
							layout: 'form',
							items: [{
								xtype: 'textfield',
								fieldLabel: '真实姓名',
								name: 'RealName',
								anchor: '90%',
								readOnly: true
							}]
						},{
							columnWidth: .5,
							layout: 'form',
							items: [{
								xtype: 'combo',
								store: genderStore(),
								displayField:'GenderT',
								valueField:'GenderTID',
								fieldLabel: '性别',
								name: 'Sex',
								hiddenName: 'Sex',
								id: 'se_o_sex',
								mode: 'local',
								triggerAction: 'all',
								blankText:'请选择性别',
								emptyText:'选择性别',
								anchor:'85%',
							}]
						},{
							columnWidth: .5,
							layout: 'form',
							items: [{
								xtype: 'datefield',
								fieldLabel: '出生日期',
								name: 'Birthday',
								enableKeyEvents : true,
								format : 'Y-m-d H:i:s',
								value:new Date()
							}]
						},{
							columnWidth: 1,
							layout: 'form',
							items: [{
								xtype:'textfield',
								fieldLabel: '密保问题',
								name: 'Pwd_Question',
								anchor: '90%'
							}]
						},{
							columnWidth: 1,
							layout: 'form',
							items: [{
								xtype: 'textfield',
								fieldLabel: '密保答案',
								name: 'Pwd_Answer',
								anchor: '90%'
							}]
						},{
							columnWidth: .5,
							layout: 'form',
							items: [{
								xtype: 'textfield',
								fieldLabel: '信用额度',
								name: 'credit_line',
								id: 'credit_line',
								anchor: '90%',
								validator: function(){
									var v = Ext.get("credit_line").dom.value;
									if((/\d+(\.\d+)?/).test(v)) {
										return true;
									}
									return false;
								},
								invalidText: '格式不正确'
								
							}]
						},{
							columnWidth: .5,
							layout: 'form',
							items: [{
								xtype: 'textfield',
								fieldLabel: '用户余额',
								name: 'user_money',
								anchor: '90%',
								readOnly: true
							}]
						},{
							columnWidth: .5,
							layout: 'form',
							items: [{
								xtype: 'textfield',
								fieldLabel: '礼金金额',
								name: 'coupon_money',
								anchor: '90%',
								readOnly: true
							}]
						},{
							columnWidth: .5,
							layout: 'form',
							items: [{
								xtype: 'textfield',
								fieldLabel: '积分',
								name: 'Points',
								anchor: '90%',
								readOnly: true
							}]
						},{
							columnWidth: 1,
							layout: 'form',
							items: [{
								xtype:'textarea',
								fieldLabel: '描述',
								width:420,
								height:60,
								name: 'Remark'
							}]
						}]
					},{
						title: '个人财务',
						border: false,
						items: [{
							
						}]
					},{
						title: '收货地址',
						border: false,
						layout: 'fit',
						height: 240,
						items: new addresscore()
					}]
				}
			]
		},
		{xtype:'hidden',name: 'MemberID',id:'MemberID'},
		{xtype:'hidden',name:'LockStatus',id:'se_o_lockstatus'}
	];

	//保存资料
	var save = function(str){
		if (!this.form.getForm().isValid()){
			return;
		}
		var selectedId = this.getSelectedIds(this.keyField);

		if(str){selectedId = 0;}
		if(selectedId>0){
			this.form.findById('MemberID').setValue(selectedId);
		}
		showLoading(c.g(c.e.save),c.loading);
		zyt.ajax(_C_Member_S,this.form.getForm().getValues(), function(){
			hideLoading();
			this.window.hide();
			Ext.HandleTip.msg(c.opres,c.item +  c.modify +c.ok);
			this.reload();
		}.createDelegate(this));
	};
	//修改密码表单项
	this.formPassItems=[{
			inputType:'password',
			fieldLabel: '新密码',
			name: 'LoginPass',
			id:'se_o_loginpass',
			anchor:'85%',
			allowBlank:false
		},{
			inputType:'password',
			fieldLabel: '确认密码',
			name: 'NewLoginPass',
			id:'se_o_loginpass2',
			anchor:'85%',
			allowBlank:false
		},
		{xtype:'hidden',name: 'MemberID',id:'MemberID'},
		{xtype:'hidden',name: 'a',id:'se_o_cmdid'},
		{xtype:'hidden',name: 'li',id:'se_o_li'},
		{xtype:'hidden',name: 'ln',id:'se_o_ln'}
	];
	this.showPassWin = function(){
		zyt.Permit();
		this.form = new Ext.FormPanel({
				labelAlign:'left',
				bodyStyle:'padding:5px',
				frame:true,
				labelWidth:60,
				items:[
					{
						layout:'form',
						defaultType:'textfield',
						border:false,
						labelSeparator:'：',
						items:this.formPassItems
				}]
		});
		this.form.getForm().reset();
		this.window = new Ext.Window({
			xtype:'window',
			title:'修改密码',
			buttonAlign : 'right',
			closable:true,
			resizable: false,
			layout:'fit',
			modal: 'true',
			width:320,
			height:150,
			closeAction:'hide',
			plain: true,
			deferredRender: true,
			items:[this.form],
			
			buttons: [{
				id: 'btnSave',
				text: c.bsave,
				handler: savepass.createDelegate(this),
				disabled: false
			},{
				text: c.bcancel,
				handler: function(){
					this.window.hide();
				}.createDelegate(this)
			}]
		});
		this.window.on('hide',function(){
			this.form.destroy();
		}.createDelegate(this));
		this.window.show();
		this.form.findById('se_o_loginpass').focus(false,true);
	}

	//保存密码
	var savepass = function(){
		if (!this.form.getForm().isValid()){
			return;
		}
		var pass = this.form.findById('se_o_loginpass').getValue();
		var pass2 = this.form.findById('se_o_loginpass2').getValue();
		if(pass!=pass2){
			alertTip('两次输入密码不一致，请确认后重新输入');
			return false;
		}
		var selectedId = this.getSelectedIds(this.keyField);
		this.form.findById('MemberID').setValue(selectedId);
		this.form.findById('se_o_cmdid').setValue(8);
		showLoading(c.g(c.e.save),c.loading);
		zyt.ajax(_C_Member_S_Pwd,this.form.getForm().getValues(), function(){
			hideLoading();
			this.window.hide();
			var act = c.modify;
			Ext.HandleTip.msg(c.opres,'密码'+ act +c.ok);
			this.reload();
		}.createDelegate(this));
	};
	this.viewReader = [
		{name: 'MemberID'},
		{name: 'Email'},
		{name: 'Mobile'},
		{name: 'UserName'},
		{name: 'MemberType'},
		{name: 'Rank'},
		{name: 'RegsterTime'},
		{name: 'LastLogin'},
		{name: 'LastIp'},
		{name: 'LoginTimes'},
		{name: 'IsOnline'},
		{name: 'OptionSet'},
		{name: 'Status'},
		{name: 'LockStatus'},
		{name: 'RealName'},
		{name: 'Sex'},
		{name: 'Birthday'},
		{name: 'Picture'},
		{name: 'credit_line'},
		{name: 'Pwd_Question'},
		{name: 'Pwd_Answer'},
		{name: 'user_money'},
		{name: 'coupon_money'},
		{name: 'Points'},
		{name: 'Remark'}
	];
	this.showView = function(str){
		zyt.Permit();
		this.form = new Ext.FormPanel({
			defaults: {width:650},
			labelAlign:'left',
			bodyStyle:'padding:5px',
			frame:true,
			labelWidth:60,
			reader : new Ext.data.JsonReader({
				root : 'record',
				success: '@success'
			},this.viewReader),
			items:[
				{
					layout:'column',
					border:false,
					labelSeparator:'：',
					items:this.viewItems
			}]
		});
		var buttons = [];
		if(!str) {
			buttons.push({
				id: 'btnSave',
				text: c.bsave,
				handler: save.createDelegate(this,[false]),
				disabled: false
			});
		}

		buttons.push({
				text: (str ? c.close : c.bcancel),
				handler: function(){
					this.window.hide();
				}.createDelegate(this)
		});
		this.window = new Ext.Window({
			xtype:'window',
			title: c.vd() + " #" + this.getSelectionModel().getSelected().data.Email,
			buttonAlign : 'right',
			closable:true,
			resizable: false,
			modal: 'true',
			layout:'fit',
			width:700,
			height:520,
			closeAction:'hide',
			iconCls:'view',
			plain: true,
			items:[],
			buttons: buttons
		});
		
		this.window.on('hide',function(){
			this.form.destroy();
		}.createDelegate(this));
		this.window.on('show', function(){
			this.getEl().mask('loading', 'x-mask-loading');
		});
		this.window.show();

		this.window.add(this.form);
		this.window.render();
		/* local mode */
		this.window.getEl().unmask();
		var memberid = this.getSelectedIds(this.keyField);
		this.form.getForm().load({
			url:_C_Member_X_Detial,
			method:'POST',
			params:'a=7&MemberID=' + memberid,
			waitMsg:'正在获取数据，请稍候...'
		});
		//this.form.getForm().setValues(this.getSelectionModel().getSelected().data);
		
	}

	var remove = function(){
		zyt.Permit();
		var delids = this.getSelectedIds(this.keyField);
		var title = (Ext.isNumber(delids) ? "（" + this.getSelectionModel().getSelected().data.Email + "）" : "");
		zyt.confirmEx(c.del, title,function(){
			zyt.ajax(_C_Member_X_Del, {a:4,aid:delids}, function(){
				Ext.HandleTip.msg(c.opres,c.selitem + c.del + c.ok);
				this.reload();
			}.createDelegate(this));
		}.createDelegate(this));
	}

	var lock = function(str){
		zyt.Permit();
		var act=str ? c.lock:c.unlock;
		Ext.MessageBox.confirm(c.info,'您确定要将所选'+act,function(btn){
			if(btn=='yes'){
				var delids = this.getSelectedIds(this.keyField);
				zyt.ajax(_C_Member_X_Lock, {a:2,aid:delids}, function(){
					Ext.HandleTip.msg('操作结果',c.selitem + act + c.ok);
					this.reload();
				}.createDelegate(this));
			}
		}.createDelegate(this));
	}
	
	var gridreader = new Ext.data.JsonReader({
		totalProperty:'totalProperty',
		root:'records'
	},this.gridStore);

	membercore.superclass.constructor.call(this, {
		region: 'center',
		store: this.newStore({url:_C_Member_X, reader:gridreader}),
		checkbox:true,
		page:true,
		keyField:'MemberID',
		viewConfig: {forceFit:true},
		tbar:[{
				id:'btnModify',
				text:c.modify + '资料',
				iconCls:'modify',
				disabled:true,
				handler: this.showView.createDelegate(this, [false])
			},'-',{
				id:'btnModifyPass',
				text:c.modify + '密码',
				iconCls:'modify',
				disabled:true,
				handler:this.showPassWin.createDelegate(this)
			},'-',{
				id:'btnDel',
				text:c.del,
				iconCls:'remove',
				disabled:true,
				handler:remove.createDelegate(this)
			},'-',{
				id:'btnLock',
				text:c.lock,
				iconCls:'lock',
				disabled:true,
				handler:lock.createDelegate(this)
			},'-',{
				id:'btnView',
				text : c.view,
				iconCls:'view',
				disabled:true,
				handler: this.showView.createDelegate(this, [true])
			}, '-',new Ext.form.TextField({
				id:'c_o_email',
				emptyText:'请输入邮箱或手机号'
			}),{
				id:'btnSearch',
				text:'搜索',
				iconCls:'search',
				handler:function(){
					var email = Ext.getCmp('c_o_email').getValue();
					var oCondition = {email:email};
					this.reload(oCondition);
				}.createDelegate(this)
		},'-']
	});

	this.getSelectionModel().on('selectionchange', function(){
		var haveselected = this.getSelectedIds(this.keyField);
		if(haveselected!='0'){
			if(typeof haveselected==='number'){
				Ext.getCmp('btnModify').setDisabled(false);
				Ext.getCmp('btnModifyPass').setDisabled(false);
				Ext.getCmp('btnView').setDisabled(false);
			}else{
				Ext.getCmp('btnModify').setDisabled(true);
				Ext.getCmp('btnModifyPass').setDisabled(true);
				Ext.getCmp('btnView').setDisabled(true);
			}
			Ext.getCmp('btnDel').setDisabled(false);
			Ext.getCmp('btnLock').setDisabled(false);
		}else{
			Ext.getCmp('btnModify').setDisabled(true);
			Ext.getCmp('btnModifyPass').setDisabled(true);
			Ext.getCmp('btnDel').setDisabled(true);
			Ext.getCmp('btnView').setDisabled(true);
			Ext.getCmp('btnLock').setDisabled(true);
		}
	}.createDelegate(this));
	
	var onContextMenu = function(grid,rowIndex,e){
		e.stopEvent();
		grid.getSelectionModel().selectRow(rowIndex);
		var messageContextMenu = new Ext.menu.Menu({
			id: 'messageContextMenu',
			items : [
			{
				id:'rView',
				text : c.view,
				iconCls:'view',
				handler:this.showView.createDelegate(this,[true])
			},{
				id:'rModify',
				text : c.modify,
				iconCls:'modify',
				handler:this.showView.createDelegate(this, [false])
			},{
				id:'rDel',
				text : c.del,
				iconCls:'remove',
				handler:remove.createDelegate(this)
			}]
		});
		messageContextMenu.showAt(e.getXY());
		e.preventDefault();
	};
	this.on('rowcontextmenu',onContextMenu);

	this.on("rowdblclick", function(grid,rowIndex){
		this.showView(true);
	});
	//返回多记录集时处理方法（例如获取权限值处理功能按钮）
	this.store.on('load',function(){
		var acl = gridreader.jsonData.Acl;
		if(typeof acl!="undefined"){
			var gridTb = this.getTopToolbar();
			if(acl[0].aNew===0){
				//gridTb.items.get(0).hide();
				//gridTb.items.get(1).hide();
				//messageContextMenu.items.get(1).hide();
			}
			if(acl[0].aEdit===0){
				//gridTb.items.get(2).hide();
				//gridTb.items.get(3).hide();
				//messageContextMenu.items.get(2).hide();
			}
			if(acl[0].aDel===0){
				//gridTb.items.get(4).hide();
				//gridTb.items.get(5).hide();
				//messageContextMenu.items.get(3).hide();
			}
		}
	}.createDelegate(this));
};
Ext.extend(membercore,Ext.ux.grid.GridPanel);

